package GreedyAlgorithm;

import com.sun.xml.internal.ws.api.message.SuppressAutomaticWSARequestHeadersFeature;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 一个字符串数组，拼成一个字典序最小的字符串
 *
 * @author Liaorun
 */
public class LowestLexicography {

    /**
     * 字符串按照 a + b <= b + a的方式排序
     */
    public static class MyComparator implements Comparator<String> {

        @Override
        public int compare(String o1, String o2) {
            return (o1 + o2).compareTo(o2 + o1);
        }
    }


    public static String lowestString(String[] strs) {
        if (strs == null || strs.length == 0) {
            return "";
        }
        Arrays.sort(strs, new MyComparator());

        String res = "";
        for (String str : strs) {
            res += str;
        }
        return res;
    }

    public static void main(String[] args) {
        String[] test = {"jibw", "ji", "jp", "bw", "jibw"};

        System.out.println(lowestString(test));
    }
}
